package com.tadpole.music.view.activity.me.yun;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tadpole.music.R;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.yun.YunLoginIView;
import com.tadpole.music.presenter.me.yun.YunLoginPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.StatusBarUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.widget.YunWenPopup;

/**
 * 蝌蚪云考试 登录
 * Created By 孙岩
 * on 2021/2/9
 */
public class YunLoginActivity extends BaseActivity implements YunLoginIView {
    //输入准考证号
    private EditText etCardNumber;
    //登录
    private TextView tvLogin;
    //问号
    private ImageView ivWen;
    //登录
    private YunLoginPresenter yunLoginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_yun);
        //将头部状态栏置为透明
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        initViews();
        initListeners();

        yunLoginPresenter = new YunLoginPresenter();
        yunLoginPresenter.attachView(this);
    }

    //初始化控件
    private void initViews(){
        etCardNumber = findViewById(R.id.etCardNumber);
        tvLogin = findViewById(R.id.tvLogin);
        ivWen = findViewById(R.id.ivWen);
    }

    //初始化监听器
    private void initListeners(){
        tvLogin.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if(etCardNumber.getText().toString().trim().equals("")){
                        ToastUtils.show("请输入准考证号");
                    }else{
                        yunLoginPresenter.yunLogin(etCardNumber.getText().toString().trim());
                    }
                }else{
                    ToastUtils.show(getResources().getString(R.string.net_work));
                }
            }
        });

        ivWen.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                showWenPopup();
            }
        });
    }

    private void showWenPopup(){
        String url = UrlConfig.getWen;
        YunWenPopup yunWenPopup = new YunWenPopup(YunLoginActivity.this,url);
        yunWenPopup.setPopupWindowFullScreen(true);
        yunWenPopup.setDismissWhenTouchOutside(false);
        yunWenPopup.showPopupWindow();
    }

    @Override
    public void showLoginResult(String token) {
        SpUtil.getInstance(YunLoginActivity.this).putString(Constant.YUN_TOKEN,token);
        Intent intent = new Intent(YunLoginActivity.this,YunStudentInfoActivity.class);
        startActivity(intent);
        this.finish();
    }
}
